package com.wg.dome.handle;

import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.entity.config.InitShopEntity;
import com.wg.dome.entity.user.ItemEntity;
import com.wg.dome.entity.user.PlayerEntity;
import com.wg.dome.operation.BuyGoodsRequest;
import com.wg.dome.operation.BuyGoodsResponse;
import com.wg.dome.repository.InitShopRepository;
import com.wg.dome.repository.ItemRepository;
import com.wg.dome.repository.PlayerRepository;
import com.wg.core.net.OperationRequest;
import com.wg.core.net.OperationResponse;
import com.wg.core.net.SendParameters;

/**
 * 购买商品
 */
public class HandleRequest17 extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {

        BuyGoodsRequest buyGoodsRequest = new BuyGoodsRequest(request);

        if (!buyGoodsRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(buyGoodsRequest);
        }

        if (buyGoodsRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, buyGoodsRequest.getPlayerId(), peer.getPlayerId());
        }

        PlayerRepository playerRepository = new PlayerRepository();
        PlayerEntity playerEntity = playerRepository.get(buyGoodsRequest.getPlayerId());

        InitShopRepository initShopRepository = new InitShopRepository();
        InitShopEntity entity = initShopRepository.get(buyGoodsRequest.getGoodsId());
        if (entity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.ITEM_ERROR, buyGoodsRequest.getPlayerId(), peer.getPlayerId());
        }

        if (buyGoodsRequest.getCount() < 0 || buyGoodsRequest.getCount() > entity.getMaxCount()){
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.GOODS_COUNT_ERROR, buyGoodsRequest.getPlayerId(), peer.getPlayerId());
        }

        long money = 0;
        switch (entity.getType()) {
            case 1:
                if (playerEntity.getGold() < entity.getPrice() * buyGoodsRequest.getCount()) {
                    return ErrorHelper.getErrorResponse(request.getOperationCode(),
                            ErrorCode.GOLD_LIMIT, buyGoodsRequest.getPlayerId(), peer.getPlayerId());
                }
                money = playerEntity.getGold() - (entity.getPrice() * buyGoodsRequest.getCount());
                playerEntity.setGold(money);
                break;
            case 2:
                if (playerEntity.getRmb() < entity.getPrice() * buyGoodsRequest.getCount()) {
                    return ErrorHelper.getErrorResponse(request.getOperationCode(),
                            ErrorCode.RMB_LIMIT, buyGoodsRequest.getPlayerId(), peer.getPlayerId());
                }
                money = playerEntity.getRmb() - (entity.getPrice() * buyGoodsRequest.getCount());
                playerEntity.setRmb(money);
                break;
            default:
                break;
        }
        playerRepository.update(playerEntity);

        ItemRepository itemRepository = new ItemRepository();
        ItemEntity item = itemRepository.getItem(buyGoodsRequest.getPlayerId(), entity.getItemId());
        if (item == null){
            item = new ItemEntity();
            item.setPlayerId(buyGoodsRequest.getPlayerId());
            item.setsId(entity.getItemId());
            item.setCount(buyGoodsRequest.getCount());
            itemRepository.add(item);
        }else {
            item.setCount(item.getCount() + buyGoodsRequest.getCount());
            itemRepository.update(item);
        }

        BuyGoodsResponse response = new BuyGoodsResponse();
        response.setMoney(money);
        response.setGoodsId(item.getsId());
        response.setCount(item.getCount());

        return new OperationResponse(request.getOperationCode(), response);
    }
}
